cd "" // set working directory
clear all

//if needed, install coefplot and unique modules
//ssc install coefplot
//ssc install unique

// bring in data from the 2018 experiment
insheet using long_data_2018.csv
tostring anonid, replace
tempfile data2018
save `data2018'

// bring in data from the 2017 experiment
insheet using "long_data.csv", clear
tostring anonid, replace
append using `data2018'

// Drop people for whom we do not have responses to the perception question
drop if missing(dv_const)
count if data2018 != 1
unique anonid if data2018 != 1

count if data2018 == 1
unique anonid if data2018 == 1

// Counts for figure
tab treat data2018, mis
tab treat data2018 if inlist(dv, "abortion", "crime", "enviro", "transit", "gun"), mis
preserve
	duplicates drop anonid treat, force
	tab treat data2018, mis
restore
preserve
	keep if inlist(dv, "abortion", "crime", "enviro", "transit", "gun")
	duplicates drop anonid treat, force
	tab treat data2018, mis
restore

//Treat 2 = district
//Treat 1 = placebo
//Treat 0 = control

local covars democrat upper trump_percent

// Treatment vs. Control
areg dv_const treat `covars' if treat != 1 & data2018 != 1, a(state) cluster(anonid)
estimates store control_all

areg dv_const treat `covars' if treat != 1 & ////
	inlist(dv, "abortion", "crime", "enviro", "transit", "gun") ///
	 & data2018 != 1, a(state) cluster(anonid)
estimates store control_stateonly

areg dv_const treat `covars' if treat != 1, a(state) cluster(anonid)
estimates store control_all_2018

areg dv_const treat `covars' if treat != 1 & ////
	inlist(dv, "abortion", "crime", "enviro", "transit", "gun"), a(state) cluster(anonid)
estimates store control_stateonly_2018

// Treatment vs. Placebo
areg dv_const treat `covars' if treat != 0 & data2018 != 1, a(state) cluster(anonid)
estimates store placebo_all

areg dv_const treat `covars' if treat != 0 & ////
	inlist(dv, "abortion", "crime", "enviro", "transit", "gun") ///
	 & data2018 != 1, a(state) cluster(anonid)
estimates store placebo_stateonly

areg dv_const treat `covars' if treat != 0, a(state) cluster(anonid)
estimates store placebo_all_2018

areg dv_const treat `covars' if treat != 0 & ////
	inlist(dv, "abortion", "crime", "enviro", "transit", "gun"), a(state) cluster(anonid)
estimates store placebo_stateonly_2018


coefplot (control_all, label(All issues) msymbol(Dh)) ////
		(control_stateonly, label(State issues only) msymbol(Oh)) ////
		(control_all_2018, label(All issues, with 2018) msymbol(Sh)) ////
		(control_stateonly_2018, label(State issues only, with 2018) msymbol(Th)) ////
		, bylabel(Compared to Control) ////
	|| (placebo_all) (placebo_stateonly) (placebo_all_2018) (placebo_stateonly_2018), ////
		bylabel(Compared to Placebo) || ////
	, keep(treat) scheme(plotplainblind) ylabel(none) ////
	xtitle("Effect of Policy Information on Misperceptions") ////
	xline(0, lp(dash) lc(gs4))
graph export district_pulse_results.png, replace

// Manipulation check
// How do people who explicitly recall district pulse do?
tabstat dv_const if recall_remember, by(treat) s(mean n)
